package com.jinzhi.jzweb.service.impl;

import com.jinzhi.jzapi.domain.UserDO;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.utils.Result;
import com.jinzhi.jzweb.dao.JazGroupProdDao;
import com.jinzhi.jzweb.dao.JazShippingAddressDao;
import com.jinzhi.jzweb.domain.*;
import com.jinzhi.jzweb.domain.vo.JazGroupActivityVo;
import com.jinzhi.jzweb.domain.vo.JazGroupOrderVo;
import com.jinzhi.jzweb.service.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jinzhi.jzweb.dao.JazGroupActivityDao;
import com.jinzhi.common.base.CoreServiceImpl;

import java.util.List;

import java.util.ArrayList;
import java.util.stream.Collectors;

/**
 * 
 * <pre>
 * 团购活动拼团表
 * </pre>
 * <small> 2024-05-11 13:54:15 | Long</small>
 */
@Service
public class JazGroupActivityServiceImpl extends CoreServiceImpl<JazGroupActivityDao, JazGroupActivityDO> implements JazGroupActivityService {

    @Override
    public Long getGroupActivityIdByUserIdGropProdId(Long userId, Long groupProdId) {
        return baseMapper.getGroupActivityIdByUserIdGropProdId(userId,groupProdId);
    }

    @Override
    public List<UserDO> getUserByActivityId(Long activityId) {
        return baseMapper.getUserByActivityId(activityId);
    }

    @Autowired
    private JazSysUserService jazSysUserService;
    @Autowired
    private JazGroupSpecsService jazGroupSpecsService;
    @Autowired
    private JazShippingAddressService jazShippingAddressService;
    @Autowired
    private JazShippingAddressDao jazShippingAddressDao;
    @Autowired
    private JazGroupActivityUserService jazGroupActivityUserService;
    @Autowired
    private JazGroupOrderService jazGroupOrderService;
    @Autowired
    private JazGroupProdDao jazGroupProdDao;

    @Override
    public Result<Page<JazGroupActivityDO>> selGroupActivityList(String name, String mobile, int current, int size, String createTime, String endTime) {
        Page<JazGroupActivityDO> page = new Page<>(current, size);
        List<JazGroupActivityDO> voList=new ArrayList<>();
        Wrapper<JazGroupActivityDO> wrapper=new EntityWrapper<>();

        //一个手机号或者一个名字会有多个订单
        if (StringUtils.isNotBlank(mobile)) {
            List<UserDO> user = jazSysUserService.selectList(new EntityWrapper<UserDO>().like("mobile", "%"+mobile+"%"));
            if (!user.isEmpty()) {
                List<Long> userIds = user.stream().map(UserDO::getId).collect(Collectors.toList());
                wrapper.in("sponsor_user_id", userIds);
            } else {
                return Result.ok(new Page<>());
            }
        }
        if (StringUtils.isNotBlank(name)){
            List<UserDO> users = jazSysUserService.selectList(new EntityWrapper<UserDO>().like("username", "%"+name+"%"));
            if (!users.isEmpty()) {
                List<Long> userIds = users.stream().map(UserDO::getId).collect(Collectors.toList());
                wrapper.in("sponsor_user_id", userIds);
            } else {
                return Result.ok(new Page<>());
            }
        }
        if(StringUtils.isNotBlank(createTime) &&  StringUtils.isNotBlank(endTime)){
            wrapper.between("create_time",createTime,endTime);
        }
        wrapper.orderBy("create_time",false);
        Page<JazGroupActivityDO> jazGroupActivityDOPage = this.selectPage(page, wrapper);
        List<JazGroupOrderVo> orderVos=new ArrayList<>();
        for (JazGroupActivityDO jazGroupActivityDO:jazGroupActivityDOPage.getRecords()){
            JazGroupActivityDO vo=new JazGroupActivityDO();
            BeanUtils.copyProperties(jazGroupActivityDO,vo);
            JazGroupProdDO jazGroupProdDO = jazGroupProdDao.selectById(jazGroupActivityDO.getGroupProdId());

            vo.setGroupProdName(jazGroupProdDO.getName());
            UserDO user = jazSysUserService.selectById(jazGroupActivityDO.getSponsorUserId());
            vo.setSponsorUserName(user.getUsername());
            vo.setSponsorUserPhone(user.getMobile());
            //查询出所有的子订单
            List<JazGroupActivityUserDO> userDOS = jazGroupActivityUserService.selectList(new EntityWrapper<JazGroupActivityUserDO>().eq("group_activity_id", jazGroupActivityDO.getId()));
            for (JazGroupActivityUserDO jazGroupActivityUserDO:userDOS){
                JazGroupOrderVo vo1=new JazGroupOrderVo();
                //根据用户查出订单
                JazGroupOrderDO jazGroupOrderDO = jazGroupOrderService.selectOne(new EntityWrapper<JazGroupOrderDO>().eq("user_id", jazGroupActivityUserDO.getUserId()).ne("wx_status","未支付"));
                if (jazGroupOrderDO!=null){
                    BeanUtils.copyProperties(jazGroupOrderDO,vo1);
                }

                //查询出所有的用户
                UserDO user1= jazSysUserService.selectById(jazGroupActivityUserDO.getUserId());
                vo1.setUserName(user1.getUsername());
                vo1.setPhone(user1.getMobile());
                orderVos.add(vo1);

            }
            vo.setPeopleNum(userDOS.size());
            vo.setOrderVos(orderVos);
            voList.add(vo);
        }
        jazGroupActivityDOPage.setRecords(voList);
        return Result.ok(jazGroupActivityDOPage);
    }

    @Override
    public JazGroupActivityVo getInfo(Long id) {
        JazGroupActivityDO jazGroupActivityDO = this.selectById(id);
        JazGroupActivityVo vo=new JazGroupActivityVo();
        BeanUtils.copyProperties(jazGroupActivityDO,vo);
        List<JazGroupOrderVo> orderVos=new ArrayList<>();
        //查询出所有的子订单
        List<JazGroupActivityUserDO> userDOS = jazGroupActivityUserService.selectList(new EntityWrapper<JazGroupActivityUserDO>().eq("group_activity_id", jazGroupActivityDO.getId()));
        for (JazGroupActivityUserDO jazGroupActivityUserDO:userDOS){
            JazGroupOrderVo vo1=new JazGroupOrderVo();
            //根据用户查出订单
            JazGroupOrderDO jazGroupOrderDO = jazGroupOrderService.selectOne(new EntityWrapper<JazGroupOrderDO>().eq("user_id", jazGroupActivityUserDO.getUserId()).eq("group_activity_id", jazGroupActivityUserDO.getGroupActivityId()));
            if (jazGroupOrderDO!=null){
                BeanUtils.copyProperties(jazGroupOrderDO,vo1);

                UserDO user = jazSysUserService.selectById(jazGroupOrderDO.getUserId());
                //如果是发起人就加一个团长
                if (jazGroupOrderDO.getUserId().equals(jazGroupActivityDO.getSponsorUserId())){
//                    vo1.setUserName(jazGroupOrderDO.getReceivingName()+"(团长)");
                    vo1.setUserName(user.getUsername()+"(团长)");
                }else {
                    vo1.setUserName(user.getUsername());
                }

//                vo1.setPhone(jazGroupOrderDO.getPhone());
                vo1.setPhone(user.getMobile());

            }
            JazGroupSpecsDO jazGroupSpecsDO = null;
            if (jazGroupOrderDO != null) {
                jazGroupSpecsDO = jazGroupSpecsService.selectById(jazGroupOrderDO.getGroupSpecsId());
                vo1.setGroupPrice(jazGroupSpecsDO.getGroupPrice());
                vo1.setOriginalPrice(jazGroupSpecsDO.getOriginalPrice());
                vo1.setPrice(jazGroupSpecsDO.getPrice());
                vo1.setSpecsName(jazGroupSpecsDO.getName());
                orderVos.add(vo1);
            }
        }
        vo.setOrderVos(orderVos);
        return vo;
    }
}
